// pages/order/order.js
const customRequst = require('../../../common/customRequst');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,
    status: '处理中',
    orderList: [],
    btnArr_0:[
      {
      txt:'投稿进度',
      type:'progress',
      },
      {
        txt:'修改文章',
        type:'edit_work',
      }
    ],
    btnArr_1: [
      {
        txt:'投稿进度',
        type:'progress',
      },
      {
        txt:'修改文章',
        type:'edit_work',
      },
      {
        txt:'立即付款',
        type:'pay',
      }, 
    ],
    btnArr_2: [
      {
        txt:'投稿进度',
        type:'progress',
      },
      {
        txt:'修改文章',
        type:'edit_work',
      },
      {
        txt:'杂志社打款截图',
        type:'upload_image',
      },
      {
        txt:'申请退款',
        type:'refund',
      },
    ],
    btnArr_3:[
      {
        txt:'立即付款',
        type:'pay',
      }, 
    ],
    btnArr_4:[
      {
        txt:'失败原因查看',
        type:'reason',
      },
    ],

    btnArr:[],
    btnstate: '',
    page: 1,
    pagesize:15,
    isLoadAllList:false,
    orderId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    let that = this;
    if(option.type){
      that.setData({
        type:option.type
      })
    }
  },
   /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    let that = this;
    let typeKey = 'btnArr_' + that.data.type;
    that.setData({
      btnArr:that.data[typeKey],
      orderList:[],
      page:1,
      isLoadAllList:false,
    })
    this.getOrderList()
  },
  //切换类型
  handleType(event) {
    let that = this;
    let type = event.currentTarget.dataset.type;
    let btnKey = 'btnArr_'+ type;
    let btnarr = that.data[btnKey];
    that.setData({
      type: type,
      page: 1,
      orderList:[],
      isLoadAllList:false,
      btnArr:btnarr,
    });
    this.getOrderList()
  },

  //触底加载
  scrollSearchList:function(){
    let that = this;
    if(that.data.isLoadAllList){
      return;
    }
    that.setData({
      page:parseInt(that.data.page) + 1
    });
    that.getOrderList();
  },
  //获取订单列表
  getOrderList() {
    let that = this
    customRequst.postRequest('api/contribution/orderlist', {
      status: that.data.type,
      page: that.data.page,
      pagesize: that.data.pagesize
    }).then(function(res){
      if(res.code == 1){
        if(res.data.length == ''){
          that.setData({
            isLoadAllList:true
          });
          return false;
        }
        let orderList = that.data.orderList;
        for(let i in res.data){
          orderList.push(res.data[i]);
        }
        that.setData({
          orderList:orderList
        })
      }else{  
        wx.showToast({
          title: res.msg,
        })
      }
    })
  },

  //按钮触发
  switch: function (e) {
    let that = this
    let type = e.currentTarget.dataset.type;
    let orderId = e.currentTarget.dataset.id;
    let url;
    switch (type) {
      case 'pay':
          if(that.data.type == 1){
            //跳转支付界面
            url = '/pages/order/pay/index?id='+orderId;
            wx.navigateTo({
              url: url,
            });
          }else if(that.data.type == 3){
            //创建差价订单
            customRequst.postRequest('api/contribution/differencepay',{id:orderId}).then(function(re){
              if(re.code ==1){
                //跳转支付界面
                url = '/pages/order/pay/index?id='+re.data.id;
                wx.navigateTo({
                  url: url,
                });
              }else{
                  wx.showToast({
                    title:re.msg,
                    icon:none
                  })
              }
            })
          }

         
        break;
        case 'progress':
          //跳转投稿进度
          wx.navigateTo({
            url: '/pages/contribution/contribution?orderid=' + orderId
          })
        break;
        case 'edit_work':
          //跳转文章修改
          url ='/pages/order/edit_order/index?id='+orderId;
          wx.navigateTo({
            url: url
          })
        break;
        case 'upload_image':
          //跳转上传支付截图n
          url ='/pages/order/upload_image/index?id='+orderId;
          wx.navigateTo({
            url: url
          })
        break;
        case 'refund':
          //跳转退款申请
          url ='/pages/order/refund/index?id='+orderId;
          wx.navigateTo({
            url: url
          })
        break;     
        case 'reason':
          //跳转退款不通过原因查看
          url ='/pages/order/reason/index?id='+orderId;
          wx.navigateTo({
            url: url
          })
        break;  
        
    }
  },
})